package com.yuneec.fly.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.yuneec.droneservice.command.ExitBind;
import com.yuneec.fly.R;
import com.yuneec.fly.events.EventBusFactory;
import com.yuneec.fly.events.commandEvent.SendCommandEvent;
import com.yuneec.fly.events.uiEvent.BindEvent;
import com.yuneec.fly.events.uiEvent.ResetEvent;
import com.yuneec.fly.events.uiEvent.ScanEvent;
import com.yuneec.fly.presenters.AbstractBindPresenter;
import com.yuneec.fly.utils.LogUtils;
import com.yuneec.fly.utils.inject.annotation.ViewInject;
import com.yuneec.fly.utils.inject.annotation.event.OnClick;
import com.yuneec.fly.utils.inject.annotation.event.OnRadioGroupCheckedChange;

/**
 * Created by jw.gu on 2016/7/13.
 */
public class BindDroneFragment extends BaseFragment {
    @ViewInject(R.id.tab)
    private RadioGroup tab;
    private BindModelFragment modelFragment;
    private BindCameraFragment cameraFragment;
    private AbstractBindPresenter presenter;

    public BindDroneFragment() {
        super();
        modelFragment = new BindModelFragment();
        cameraFragment = new BindCameraFragment();
    }

    @Override
    protected int getContentViewResourceId() {
        return R.layout.bind_setting;
    }

    @Override
    public void init(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        tab.check(R.id.model);
    }

    private void switchTFragment(BaseFragment fragment) {
        getFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
    }

    @OnRadioGroupCheckedChange(R.id.tab)
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.model:
                presenter = modelFragment.presenter;
                switchTFragment(modelFragment);
                break;
            case R.id.camera:
                presenter = cameraFragment.presenter;
                switchTFragment(cameraFragment);
                break;
        }
    }

    private long clickTime;

    @OnClick({R.id.reflush, R.id.reset, R.id.bind})
    public void onClick(View v) {
        long t = System.currentTimeMillis();
        if (t - clickTime < 300) {
            return;
        }
        clickTime = t;
        switch (v.getId()) {
            case R.id.reflush:
                EventBusFactory.getUiBus().post(new ScanEvent());
                break;
            case R.id.reset:
                EventBusFactory.getUiBus().post(new ResetEvent());
                break;
            case R.id.bind:
                if (presenter.mChecked != -1) {
                    EventBusFactory.getUiBus().post(new BindEvent());
                }
                break;
        }
    }

    @Override
    public void onDestroyView() {
        LogUtils.e("==================== exit bind");
        EventBusFactory.getCommandBus().post(new SendCommandEvent(new ExitBind(), null));
        super.onDestroyView();
    }
}
